#! /bin/sh

# Kenton's Code Playground -- http://code.google.com/p/kentons-code
# Author: Kenton Varda (temporal@gmail.com)
# Copyright (c) 2010 Google, Inc. and contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu

if test $# = 0; then
  # Ekam is querying the script.  Tell it that we like C++ source files.
  echo trigger filetype:.ekam-manifest
  exit 0
fi

INPUT=$1

echo findProvider file:$INPUT
read INPUT_DISK_PATH

exec 3<$INPUT_DISK_PATH

DIRNAME=$(dirname $INPUT)

while read FILE TARGET <&3; do
  FILE=$DIRNAME/$FILE
  echo findInput $FILE
  read FILE_DISK_PATH
  
  if test "$FILE_DISK_PATH" = ""; then
    echo "$FILE: not found" >&2
    exit 1
  fi

  case $TARGET in
    */* )
      echo install $FILE_DISK_PATH $TARGET
      ;;
    * )
      echo install $FILE_DISK_PATH $TARGET/$(basename $FILE)
      ;;
  esac
done
